const app = getApp()
Page({
  data: {
    inputText: 'Hello World!',
    receiveText: '',
    name: '',
    acc_record:[{

    } ],
    connectedDeviceId: '',
    services: {},
    characteristics: {},
    connected: true,
    notified: true 
  },
  bindInput: function (e) {
    this.setData({
      inputText: e.detail.value
    })
    console.log(e.detail.value)
  },
  Send: function () {
    var that = this
    if (that.data.connected) {
      wx.getBLEDeviceServices({
        deviceId: that.data.connectedDeviceId,
        success: function (res) {
          console.log(res.services)       
          that.setData({
            services: res.services
          })
          wx.getBLEDeviceCharacteristics({
            deviceId: that.data.connectedDeviceId,
            serviceId: res.services[0].uuid,
            success: function (res) {
              console.log('获取characteristics成功')
              console.log(res.characteristics)
              that.setData({
                characteristics: res.characteristics
              })
              wx.notifyBLECharacteristicValueChange({
                state: that.data.notified,
                deviceId: that.data.connectedDeviceId,
                serviceId: that.data.services[0].uuid,
                characteristicId: that.data.characteristics[1].uuid,
                success: function (res) {
                  console.log('启用notify成功')
                  that.setData({
                    notified: !that.data.notified
                  })
                },
                fail:function(){
                  console.log('启用notify失败')
                }
                
              })
            },
            fail:function(){
              console.log('获取characteristics失败')
            }
          })
        },
        fail:function(){
          console.log('获取服务失败')
        }
      })
      wx.onBLEConnectionStateChange(function (res) {
        console.log(res.connected)
        that.setData({
          connected: res.connected
        })
      })
      wx.onBLECharacteristicValueChange(function (res) {
        var receiveText = app.buf2hex(res.value)
        console.log('接收到数据：' + receiveText)
        that.setData({
          receiveText: receiveText
        })
      })
    }
    else {
      wx.showModal({
        title: '提示',
        content: '蓝牙已断开',
        showCancel: false,
        success: function (res) {
          that.setData({
            searching: false
          })
        }
      })
    }
  },

  radioChange(e) {
    console.log('radio发生change事件，携带value值为：'+ e.detail.value)    
  },

  onLoad: function (options) {
    var that = this
    console.log(options)
    that.setData({
      name: options.name,
      connectedDeviceId: options.connectedDeviceId
    })    
  },
  onReady: function () {

  },
  onShow: function () {

  },
  onUnload(){

  },
  onHide: function () {
  }
})